package com.lepinwenhua.roomdemo;

import java.util.List;

import androidx.lifecycle.LiveData;

/**
 * @author: 魏从响
 * @date: 2025/10/27
 * @class: FruitRepository
 */
public class FruitRepository {
    private FruitDao fruitDao;
    private LiveData<List<Fruit>> allFruits;

    public FruitRepository(FruitDao fruitDao) {
        this.fruitDao = fruitDao;
        this.allFruits = fruitDao.getAllFruits(); // 直接赋值，因为已经是 LiveData
    }

    public LiveData<List<Fruit>> getAllFruits() {
        return allFruits;
    }

    // 插入操作需要在新线程中执行
    public void insert(Fruit fruit) {
        new Thread(() -> fruitDao.insert(fruit)).start();
    }

    public void insertAll(Fruit... fruits) {
        new Thread(() -> fruitDao.insertAll(fruits)).start();
    }

    public void update(Fruit fruit) {
        new Thread(() -> fruitDao.update(fruit)).start();
    }

    public void delete(Fruit fruit) {
        new Thread(() -> fruitDao.delete(fruit)).start();
    }
}

